<template>
  <div class="active">
    <el-form ref="formRef" :model="form" label-width="160px">
      <el-form-item label="项目评审开始时间">
        <el-date-picker
          v-model="projectInfo.auditTime"
          type="datetime"
          placeholder="选择日期时间"
          style="width: 500px"
          disabled
        />
      </el-form-item>
      <el-form-item label="主持人评审签到时间">
        <el-date-picker
          v-model="form.startCheckTime"
          type="datetime"
          placeholder="选择先签到"
          style="width: 500px"
          disabled
        />
        <span @click="StartSingin" class="link">主持人签到签名</span>
      </el-form-item>

      <el-form-item label="主持人评审签到签名">
        <div class="tablet">
          <el-image v-if="form.startCheck" :src="baseURL + form.startCheck">
            <template #error>
              <div class="image-slot"></div>
            </template>
          </el-image>
        </div>
      </el-form-item>

      <el-form-item label="主持人对评委授权">
        <el-input v-model="form.remarkOne.name" style="width: 500px" disabled />
        <span @click="selectJudge('remarkOne')" class="link">选评委</span>
      </el-form-item>

      <el-form-item label="">
        <el-input v-model="form.remarkTwo.name" style="width: 500px" disabled />
        <span @click="selectJudge('remarkTwo')" class="link">选评委</span>
      </el-form-item>

      <el-form-item label="">
        <el-input
          v-model="form.remarkThree.name"
          style="width: 500px"
          disabled
        />
        <span @click="selectJudge('remarkThree')" class="link">选评委</span>
      </el-form-item>
      <el-form-item label="">
        <el-input
          v-model="form.remarkFour.name"
          style="width: 500px"
          disabled
        />
        <span @click="selectJudge('remarkFour')" class="link">选评委</span>
      </el-form-item>
      <el-form-item label="">
        <el-input
          v-model="form.remarkFive.name"
          style="width: 500px"
          disabled
        />
        <span @click="selectJudge('remarkFive')" class="link">选评委</span>
      </el-form-item>

      <el-form-item label="主持人对监督人授权">
        <el-input
          v-model="form.supervisorAuthorization.name"
          style="width: 500px"
          disabled
        />
        <span @click="selectJudge('supervisorAuthorization')" class="link"
          >选监督人</span
        >
      </el-form-item>
    </el-form>

    <sxb ref="Tablet" @Return="Return" style="display: none" />
    <div class="endbutton">
      <el-button type="primary" @click="SignInPush">下一步</el-button>
    </div>
    <el-dialog :title="`选${title}`" v-model="modelhideOne">
      <el-form ref="formRef" :model="form" label-width="160px">
        <el-row :gutter="20">
          <el-col
            v-if="judgeList.length > 0"
            :span="6"
            v-for="(option, index) in judgeList"
            :key="index"
          >
            <el-radio v-model="selectedoption" :label="option">{{
              option.name
            }}</el-radio>
          </el-col>
          <el-col v-else style="text-align: center">暂无{{ title }}</el-col>
        </el-row>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="modelhideOne = false">取消</el-button>
          <el-button type="primary" @click="confirmSelection">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import {
  addJudge,
  execTask,
  queryJudge,
  delJudge,
  putJudge,
  projectInfoAndJudge,
  queryTaskId,
  auditsign,
} from "@/api/examine.js";
import { judgeProjectInfo } from "@/api/project.js";
import { getDictList, getDict, nowTime } from "@/utils/common.js";
import { useRoute, useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import sxb from "@/components/sxb.vue";

const route = useRoute();
const router = useRouter();
const Tablet = ref(null);
const selectedOption = ref({ id: "", value: "", label: "" });
const selectedoption = ref({});
const baseURL = import.meta.env.VITE_APP_BASE_API;
const title = ref("");
const modelhideOne = ref(false);
const projectInfo = ref({});
const form = ref({
  projectStartTime: "",
  hostSigninTime: "",
  hostSignature: "",
  remarkOne: {}, //评委1
  remarkTwo: {}, //评委2
  remarkThree: {}, //评委3
  remarkFour: {}, //评委4
  remarkFive: {}, //评委5
  supervisorAuthorization: {},
  fileDecryptInfo: "",
  qualificationReviewInfo: "",
  priceReviewInfo: "",
  businessReviewInfo: "",
  technicalReviewInfo: "",
  resultConfirmationInfo: "",
});
const id = computed(() => route.query.id);

const judgeList = ref([]);
const juryList = ref([]);
const custodianList = ref([]);
const confirmSelection = () => {
  modelhideOne.value = false;
  if (selectedoption.value.id && projectInfo.value[selectedOption.value]) {
    for (let i = 0; i < projectInfo.value.judgeList.length; i++) {
      if (projectInfo.value.judgeList[i].userId == selectedoption.value.id) {
        ElMessage.error("不能重复选择");
        return;
      }
    }
    putJudge({
      id: projectInfo.value[selectedOption.value].id,
      userId: selectedoption.value.id,
    }).then((res) => {
      if (res.code == "200") {
        getInfo();
      }
    });
  } else if (selectedoption.value.id) {
    for (let i = 0; i < projectInfo.value.judgeList.length; i++) {
      if (projectInfo.value.judgeList[i].userId == selectedoption.value.id) {
        ElMessage.error("不能重复选择");
        return;
      }
    }
    form.value[selectedOption.value] = selectedoption.value;
    if (form.value[selectedOption.value].type == "judge_jury") {
      applySupplier(selectedOption.value, "judge_jury");
    } else {
      applySupplier("supervisorAuthorization", "judge_custodian");
    }
  } else if (!selectedoption.value.id) {
    form.value[selectedOption.value] = {};
    delJudge({ id: projectInfo.value[selectedOption.value].id }).then((res) => {
      if (res.code == "200") {
        getInfo();
      }
    });
  }
};
const judgeId = ref("");

const userInfo = computed(() => JSON.parse(localStorage.getItem("judgeInfo")));

const Return = (item) => {
  const params = {
    projectId: id.value,
    projectCode: projectInfo.value.oaCode,
    projectJudgeId: judgeId.value,
    signStr: item,
    type: "sign_start",
  };
  auditsign(params).then((res) => {
    if (res.code == "200") {
      form.value.startCheck = res.data;
      getInfo();
    }
  });
};
const StartSingin = () => {
  Tablet.value.beginSign();
};

const selectJudge = (key) => {
  if (key == "supervisorAuthorization") {
    selectedOption.value = key;
    judgeList.value = custodianList.value;
    title.value = "监督人";
  } else {
    selectedOption.value = key;
    judgeList.value = juryList.value;
    title.value = "评委";
  }
  modelhideOne.value = true;
};

const applySupplier = (key, type) => {
  const params = {
    projectId: id.value,
    userId: form.value[key].id,
    type: type,
  };
  addJudge(params).then((res) => {
    if (res.code == "200") {
      getInfo();
    }
  });
};
const SignInPush = () => {
  try {
    nextStep(); //提交并且修改状态
  } catch (error) {}
};
const nextStep = () => {
  const params = { projectId: id.value };
  execTask(params).then((res) => {
    if (res.code) {
      window.location.reload();
    }
  });
};
const getInfo = () => {
  selectedoption.value = {};
  queryJudge({ type: "judge_jury" }).then((res) => {
    if (res.code == "200") {
      juryList.value = res.data;
    }
  });
  queryJudge({ type: "judge_custodian" }).then((res) => {
    if (res.code == "200") {
      custodianList.value = res.data;
    }
  });
  projectInfoAndJudge({ projectId: id.value }).then((res) => {
    if (res.code == "200") {
      console.log(12333);

      form.value = {
        remarkOne: {},
        remarkTwo: {},
        remarkThree: {},
        remarkFour: {},
        remarkFive: {},
        supervisorAuthorization: {},
      };
      projectInfo.value = {};
      projectInfo.value = res.data;
      for (let i = 0; i < res.data.judgeList.length; i++) {
        if (res.data.judgeList[i].userId == userInfo.value.id) {
          judgeId.value = res.data.judgeList[i].id;
          form.value.startCheckTime = res.data.judgeList[i].startCheckTime;
          form.value.startCheck = res.data.judgeList[i].startCheck;
          console.log(form.value);
        }
        if (res.data.judgeList[i].type == "judge_custodian") {
          form.value.supervisorAuthorization = {
            id: res.data.judgeList[i].id,
            name: res.data.judgeList[i].name,
          };

          projectInfo.value.supervisorAuthorization = {
            id: res.data.judgeList[i].id,
            name: res.data.judgeList[i].name,
          };
        } else if (res.data.judgeList[i].type == "judge_jury") {
          if (!form.value.remarkOne.name) {
            form.value.remarkOne = {
              id: res.data.judgeList[i].id || "",
              name: res.data.judgeList[i].name || "",
            };
            projectInfo.value.remarkOne = {
              id: res.data.judgeList[i].id || "",
              name: res.data.judgeList[i].name || "",
            };
          } else if (!form.value.remarkTwo.name) {
            form.value.remarkTwo = {
              id: res.data.judgeList[i].id || "",
              name: res.data.judgeList[i].name || "",
            };
            projectInfo.value.remarkTwo = {
              id: res.data.judgeList[i].id || "",
              name: res.data.judgeList[i].name || "",
            };
          } else if (!form.value.remarkThree.name) {
            form.value.remarkThree = {
              id: res.data.judgeList[i].id || "",
              name: res.data.judgeList[i].name || "",
            };
            projectInfo.value.remarkThree = {
              id: res.data.judgeList[i].id || "",
              name: res.data.judgeList[i].name || "",
            };
          } else if (!form.value.remarkFour.name) {
            form.value.remarkFour = {
              id: res.data.judgeList[i].id || "",
              name: res.data.judgeList[i].name || "",
            };
            projectInfo.value.remarkFour = {
              id: res.data.judgeList[i].id || "",
              name: res.data.judgeList[i].name || "",
            };
          } else if (!form.value.remarkFive.name) {
            form.value.remarkFive = {
              id: res.data.judgeList[i].id || "",
              name: res.data.judgeList[i].name || "",
            };
            projectInfo.value.remarkFive = {
              id: res.data.judgeList[i].id || "",
              name: res.data.judgeList[i].name || "",
            };
          } else {
            console.log("还有更多");
          }
        }
        if (
          !form.value.supervisorAuthorization &&
          !projectInfo.value.supervisorAuthorization
        ) {
          console.log("缺少监督人");
        }
      }
    }
  });
};
const Previous = ref("");
onMounted(() => {
  getInfo();
  queryTaskId({ projectId: id.value }).then((res) => {
    Previous.value = res.data;
  });
});
</script>
<style scoped lang="scss">
@use "./judge";
</style>
